package com.ep.commonbaseadapter.base;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.List;

/**
 * Created by Administrator on 2017/7/31.
 */

public  class MultiItemTypeBaseAdapter<T> extends BaseAdapter {
    private Context mContext;
    private List<T> mDatas;
    private ItemViewDelegateManager mItemViewDelegateManager;

    public MultiItemTypeBaseAdapter(Context context) {
        mContext = context;
        mItemViewDelegateManager = new ItemViewDelegateManager();
    }

    @Override
    public int getCount() {
        return mDatas != null ? mDatas.size() : 0;
    }

    @Override
    public T getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ItemViewDelegate itemViewDelegate = mItemViewDelegateManager.getItemViewDelegate(mDatas.get(position), position);
        int itemViewLayoutId = itemViewDelegate.getItemViewLayoutId();

        ViewHolder viewHolder=null;
        if (convertView==null){
            convertView= LayoutInflater.from(mContext).inflate(itemViewLayoutId,parent,false);
            viewHolder=new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }else {
            viewHolder= (ViewHolder) convertView.getTag();
        }

        bindData(viewHolder,mDatas.get(position),position);
        return convertView;
    }

    @Override
    public int getViewTypeCount() {
        if (useItemViewDelegateManager()) {
            return mItemViewDelegateManager.getItemViewDelegateCount();
        }
        return super.getViewTypeCount();
    }

    @Override
    public int getItemViewType(int position) {
        if (useItemViewDelegateManager()) {
            int viewType = mItemViewDelegateManager.getItemViewType(mDatas.get(position), position);
            return viewType;
        }
        return super.getItemViewType(position);
    }

    /**
     * 判断是否使用ItemViewDelegateManager
     * @return
     */
    private boolean useItemViewDelegateManager() {
        return mItemViewDelegateManager.getItemViewDelegateCount() > 0;
    }

    /**
     * 绑定数据
     * @param viewHolder
     * @param data
     * @param position
     */
    private void bindData(ViewHolder viewHolder, T data, int position){
        mItemViewDelegateManager.bindData(viewHolder,data,position);
    }

    /**
     * 添加ItemViewDelegate
     * @param itemViewDelegate
     */
    protected void addItemViewDelegate(ItemViewDelegate<T> itemViewDelegate){
        mItemViewDelegateManager.addItemViewDelete(itemViewDelegate);
    }


    /**
     * 设置数据源
     */
    public void setDatas(List<T> datas){
        this.mDatas = datas;
        this.notifyDataSetChanged();
    }

    /**
     * 追加数据
     * @param datas
     */
    public void addDatas(List<T> datas){
        this.mDatas.addAll(datas);
        this.notifyDataSetChanged();
    }

    /**
     * 删除数据源
     * @param position
     */
    public void deleteDatas(int position){
        this.mDatas.remove(position);
        this.notifyDataSetChanged();
    }
}
